package tq.com.tqteacher.model;

import android.content.Context;
import android.content.SharedPreferences;

import org.json.JSONObject;

import java.io.Serializable;

public class TQUserInfo {
    private int id;
    private String phone;
    private String name;

    private int role;
    private String pic;
    private int subject;

    private int grade;
    private String intro;
    private int clzid;

    public void refresh(JSONObject pObject){
        if(pObject == null)return;

        id = pObject.optInt("id");
        phone = pObject.optString("phone");
        name = pObject.optString("name");

        role = pObject.optInt("role");
        pic = pObject.optString("pic");
        subject = pObject.optInt("subject");

        grade = pObject.optInt("grade");
        intro = pObject.optString("intro");
        clzid = pObject.optInt("clzid");
    }

    public void copy(TQUserInfo pUserInfo){
        if(pUserInfo == null) return;
        if(pUserInfo == this) return;

        this.id = pUserInfo.id;
        this.phone = pUserInfo.phone;
        this.name = pUserInfo.name;
        this.role = pUserInfo.role;
        this.pic = pUserInfo.pic;
        this.subject = pUserInfo.subject;
        this.grade = pUserInfo.grade;
        this.intro = pUserInfo.intro;
        this.clzid = pUserInfo.clzid;
    }

    public int getId() {
        return id;
    }
    public void setId(int pId){ id = pId;}

    public String getPhone() {
        return phone;
    }

    public String getName() {
        if(name == null || name.length() == 0){
            return "匿名";
        }
        return name;
    }

    public void setName(String name){
        this.name = name;
    }
    public int getRole() {
        return role;
    }

    public String getPic() {
        return pic;
    }
    public void setPic(String pPic){
        this.pic = pPic;
    }

    public int getSubject() {
        return subject;
    }

    public int getGrade() {
        return grade;
    }

    public String getIntro() {
        return intro;
    }

    public int getClzid() {
        return clzid;
    }

    static private String SP_LOGIN_INFO = "tqLoginInfo";
    static private String SP_KEY_LOGIN_NAME = "tqLogin";
    static private String SP_KEY_LOGIN_pwd = "tqPwd";
    static public void saveLoginInfo(Context pContext, String name , String pPwd){
        SharedPreferences lPreferences = pContext.getSharedPreferences(SP_LOGIN_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor lEditor = lPreferences.edit();

        lEditor.putString(SP_KEY_LOGIN_NAME, name);
        lEditor.putString(SP_KEY_LOGIN_pwd, pPwd);

        lEditor.commit();
    }

    static public String getSaveName(Context pContext){
        SharedPreferences lPreferences = pContext.getSharedPreferences(SP_LOGIN_INFO, Context.MODE_PRIVATE);
        String name = lPreferences.getString(SP_KEY_LOGIN_NAME, null);
        return name;
    }

    static public String getSavePwd(Context pContext){
        SharedPreferences lPreferences = pContext.getSharedPreferences(SP_LOGIN_INFO, Context.MODE_PRIVATE);
        String pwd = lPreferences.getString(SP_KEY_LOGIN_pwd, null);
        return pwd;
    }
    static public void cleanSaveInfo(Context pContext){
        SharedPreferences lPreferences = pContext.getSharedPreferences(SP_LOGIN_INFO, Context.MODE_PRIVATE);
        SharedPreferences.Editor lEditor = lPreferences.edit();
        lEditor.clear();
        lEditor.commit();
    }
}
